package com.example.demo.controller;

import com.diboot.core.vo.JsonResult;
import com.diboot.file.controller.BaseExcelFileController;
import com.diboot.file.excel.listener.ReadExcelListener;
import com.diboot.file.util.ExcelHelper;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.OperationCons;
import com.example.demo.entity.OrderSku;
import com.example.demo.entity.ProcurementOrderItem;
import com.example.demo.entity.ProcurementPlanItem;
import com.example.demo.excel.listener.ProcurementPlanItemImportListener;
import com.example.demo.excel.model.ProcurementPlanItemImportModel;
import com.example.demo.service.ProcurementOrderItemService;
import com.example.demo.service.ProcurementPlanItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 项目文件 Excel处理Controller
 * @author MyName
 * @version 1.0
 * @date 2023-09-13
 * Copyright © MyCompany
 */
@RestController
@RequestMapping("/procurementPlanItem/excel")
@BindPermission(name = "采购计划项导入导出", code = "ProcurementPlanItemExcel")
@Slf4j
public class ProcurementPlanItemExcelController extends BaseExcelFileController {

    @Autowired
    private ProcurementPlanItemService procurementPlanItemService;

    /**
     * 预览数据
     *
     * @param file
     * @param params
     * @throws Exception
     */
    @BindPermission(name = "导入预览", code = OperationCons.CODE_IMPORT)
    @PostMapping("/preview")
    public JsonResult preview(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> params) throws Exception {
        return super.excelPreview(file, ProcurementPlanItem.class, params);
    }

    /**
     * 导入预览保存
     *
     * @param params
     * @throws Exception
     */
    @BindPermission(name = "导入预览保存", code = OperationCons.CODE_IMPORT)
    @PostMapping("/previewSave")
    public JsonResult previewSave(@RequestParam Map<String, Object> params) throws Exception {
        return super.excelPreviewSave(params);
    }

    /**
     * 上传excel并保存
     *
     * @param file
     * @param params
     * @throws Exception
     */
    @BindPermission(name = "直接导入", code = OperationCons.CODE_IMPORT)
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> params) throws Exception {
        return super.uploadExcelFile(file, ProcurementPlanItem.class, params);
    }

    /**
     * 下载示例文件
     *
     * @param response
     * @throws Exception
     */
    @BindPermission(name = "导入采购计划项示例文件下载", code = OperationCons.CODE_IMPORT)
    @GetMapping("/downloadExample")
    public void downloadExample(HttpServletResponse response) throws Exception{
        String fileName = "采购计划项导入示例.xlsx";
        ExcelHelper.exportExcel(response, fileName, ProcurementPlanItemImportModel.class, null);
    }

    /**
     * 项目文件列表导出
     *
     * @param response
     * @return
     * @throws Exception
     */
//    @BindPermission(name = "导出列表文件", code = OperationCons.CODE_EXPORT)
//    @GetMapping("/export")
//    public JsonResult export(OrderSkuDTO projectFileDto, @RequestParam(value = "columns", required = false) List<String> columns,
//                             HttpServletResponse response) throws Exception{
//        QueryWrapper<OrderSku> queryWrapper = super.buildQueryWrapperByQueryParams(projectFileDto);
//        List<OrderSkuListVO> projectFileList = orderSkuService.getViewObjectList(queryWrapper, null, OrderSkuListVO.class);
//        if (V.isEmpty(projectFileList)) {
//            return new JsonResult(Status.FAIL_OPERATION, "项目文件列表为空，导出失败");
//        }
//        String fileName = "项目文件列表导出_" + D.today() + ".xlsx";
//        List<OrderSkuExportModel> dataList = this.entityList2ExcelList(orderSkuList);
//        ExcelHelper.exportExcel(response, fileName, OrderSkuExportModel.class, columns, dataList);
//        return null;
//    }

    /**
     * 实体列表转excel列表
     *
     * @param projectFileList
     * @return
     */
//    private List<OrderSkuExportModel> entityList2ExcelList(List<OrderSkuListVO> projectFileList) {
//        if (V.isEmpty(projectFileList)) {
//            return Collections.emptyList();
//        }
//        return BeanUtils.convertList(projectFileList, OrderSkuExportModel.class);
//    }

    @Override
    protected ReadExcelListener<ProcurementPlanItemImportModel> getExcelDataListener() {
        return new ProcurementPlanItemImportListener();
    }
}
